"""
@name : 2021-07-27-高阶函数
@author : wenyao
@projectname: xsfh
"""
#高阶函数
#把函数作为参数传入，这样的函数称为高阶函数

# def func01(a,b):
#     print(f"{a} ... {b}")
#
# def func02(func, a,b):
#     print("strt func....")
#     func(a,b)
#
# func02(func01,"x","y")

############
#求取列表中每个元素的平方值
# lst = [1,2,3,4,5,6]
# tmp_list = []
# for i in lst:
#     tmp_list.append(i*i)
# print(tmp_list)

# lst = [1,2,3,4,5,6]
# lst2 = [3,4,5,6,7,8,9,10]
# def func(item):
#     return item*item
# # result = map(func, lst)
# result = map(lambda item:item*item, lst)
# print(list(result))
#
# def func2(item1,item2):
#     return item1*item2
# # result = map(func2, lst, lst2)
# result = map(lambda x,y: x*y, lst, lst2)
# print(list(result))

# 保留两位小数
# a = [1.1231441,1.23212121,123.2121]
# # print(round(3.6734527,3))
# result = map(lambda x:round(x,2),a)
# print(list(result))
# result = list(map(lambda item:"%.2f"%item,a))
# print(result)
# # 转化为百分比输出，并且保留两位小数
# # 0.034556 -- 》3.46%
# b = [0.034556, 0.007657, 0.8888, 0.3]
# result = map(lambda x:f"{round(x*100,2)}%",b)
# print(list(result))
# # 将a里的每个数字转化为字符串形式
# c = [1,2,3,4]
# result = map(lambda x:str(x),c)
# print(list(result))
# # 输出d字符串中每个对应的ascii码值
# d = "span"
# result = map(lambda x:ord(x),d)
# print(list(result))
# # 对列表中的每个元素取绝对值
# f = [-2,-3,0,5,-6]
# result = map(lambda x:abs(x) ,f)
# print(list(result))

#####filter
#过滤
#取出10以内的奇数
# result = filter(lambda x:x%2 == 1, range(10))
# print(list(result))

#过滤出1-100以内开平方根是整数的数
#过滤出100-999以内的水仙花数
#153  = 1的三次方 + 5的三次方 + 3的三次方
# 过滤出 1-100 以内开平方根是整数的数
import math
# result = filter(lambda x:math.sqrt(x).is_integer(),range(1,101))
# print(list(result))
# result = filter(lambda x:x == int(math.sqrt(x))*int(math.sqrt(x)) , range(101))
# print(list(result))
# result = filter(lambda x:math.sqrt(x) in range(1,11) , range(101))


# 过滤掉 100-999 以内的水仙花数
# 水仙花数： 153 = 1的三次方 + 5的三次方 + 3的三次方
# result = filter(lambda x:x ==(x//100)**3 + (x%100//10)**3 + (x%10)**3, range(100,1000))
# print(list(result))

# sorted  排序
# a = [2,3,7,3,4,2]
# # a.sort()
# 布尔类型也可以排序
# b = [1,0,True,False,len(a)>2]
# print(sorted(b))
# key指定排序的规则函数
# lst1 = [-1,0,-4,3,2]
# print(sorted(lst1, key=abs))
# 忽略大小写排序
# lst=['bob','about','Zoo','Credit']
# # print(sorted(lst))
# print(sorted(lst,key=lambda x:x.upper()))
# print(sorted(lst,key=str.upper))
# print(sorted(lst, key=len))
# 列表中包元组，先按照第一个值进行排序，第一个值都一样就按照第二个值排序，依次类推
# lst = [(1,2),(2,2),(1,0),(2,4),(True,False)]
# print(sorted(lst))
# d1 = {"c":2,"d":1,"b":3,"a":5}
# print(d1.items())
# print(dict(sorted(d1.items(),key=lambda x:x[1], reverse=True)))

#reduce
# from functools import reduce
# a = [1,2,3,4]  #-->1234
# def func1(x,y):
#     return x*10 +y
# print(reduce(func1,a))

#实现python的int功能  ‘123’ -- >123
#不能使用int函数
# b = {'1':1,"2":2,"3":3,"4":4}
# c = "422"
# d = list(map(lambda x:b[x],c))
# print(d)
# print(reduce(lambda x,y:x*10+y,d))


